/*
链接：https://ac.nowcoder.com/acm/contest/81597/I
来源：牛客网

Each time, Red can choose a subarray of consecutive cards (at least 22 cards) to remove from the deck. 
The chosen subarray must satisfy that the first and last cards have the same number. 
The score for this operation is: the number of cards multiplied by the number on the first card. 
Now Red wants to know what is the maximum value of the final score?
*/
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define endl "\n"
#define pii pair<int,int>
#define mii map<int,int>
#define mp(a,b) make_pair(a,b)
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define i128 __int128
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;

void init_inv(int size,int mod);
void init_fac(int size,int mod);
int fp(int a,int b,int mod);
int C(int m,int n,int mod);
int A(int m,int n,int mod);

vector<int> inv;
vector<int> factory;

void solve(){
    int n;
    cin>>n;
    vector<int> a(2*n+10);
    for(int i=1;i<=2*n;i++){
        cin>>a[i];
    }
    vector<int> l(n+10);
    vector<int> r(n+10);
    for(int i=1;i<=2*n;i++){ //保存每个i的区间
        if(!l[a[i]]){
            l[a[i]]=i;
        }else{
            r[a[i]]=i;
        }
    }
    r[0]=2*n+1; //0-0的区间为0 - 2*n-1
    vector<int> f(n+10); //f[i]的定义为l[i] - r[i]能取的最大值
    vector<int> g; //g[k]的定义为l[i] - k能取的最大值
    vector<pii> ui(n+1); //ui[i]的定义为区间ui[i].second的长度为ui[i].first 按照长度升序排序
    for(int i=1;i<=n;i++){
        ui[i]=mp(r[i]-l[i]+1,i);
    }
    sort(ui.begin(),ui.end());
    g.reserve(2*n+10);
    for(int h=1;h<=n;h++){ //以区间长度从小到大的形式遍历区间
        int i=ui[h].second;
        g.clear();
        g.resize(2*n+10);
        int l0=l[i]-2; //以l0为底
        for(int k=l[i]-l0;k<=r[i]-l0;k++){ //从l[i]到r[i]遍历
            g[k]=g[k-1]+i; //直接从上一状态转移
            if(l[a[k+l0]]>l[i]&&k+l0==r[a[k+l0]]){ //若当前遍历到的a[k]存在l[a[k]]>l则可以从之前转移
                g[k]=max(g[k],g[l[a[k+l0]]-l0-1]+f[a[k+l0]]);
            }
        }
        f[i]=g[r[i]-l0];
    }
    g.clear();
    g.resize(2*n+10);
    int l0=l[0]-2; //答案就为在整个区间前后加上0后的大区间的最大值
    for(int k=l[0]-l0;k<=r[0]-l0;k++){
        g[k]=g[k-1];
        if(k+l0==r[a[k+l0]]){
            g[k]=max(g[k],g[l[a[k+l0]]-l0-1]+f[a[k+l0]]);
        }
    }
    int res=g[r[0]-l0];
    cout<<res<<endl;
}

signed main(){
    int t=1;
    while(t--){
        solve();
    }
}

int fp(int a,int b,int mod){
    if(b==0){
        return 1;
    }
    int ret=1;
    while(b){
        if(b&1){
            ret=ret*a%mod;
        }
        a=a*a%mod;
        b>>=1;
    }
    return ret;
}
void init_fac(int size,int mod){
    factory.resize(size+10);
    factory[0]=1;
    for(int i=1;i<=size;i++){
        factory[i]=factory[i-1]*i%mod;
    }
}
void init_inv(int size,int mod){
    inv.resize(size+10);
    inv[1]=1;
    for(int i=2;i<=size;i++){
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    }
}
int C(int m,int n,int mod){
    int ans=1;
    for(int i=1;i<=n;i++){
        ans=ans*((m-i+1)%mod)%mod*inv[i];
    }
    return ans;
}
int A(int m,int n,int mod){
    int ans=1;
    for(int i=1;i<=n;i++){
        ans=ans*(m-i+1)%mod;
    }
    return ans;
}
